/**
 *  日期格式化
 * @param {String} date 年-月-日 时:分:秒
 * @returns String
 */

function dateFormat(date){
    //用时间戳来判断过去了多久
    //旧的时间
    var oldD = new Date(date).getTime()
    //新的时间
    var nowD = new Date().getTime() 
    //时间差：diff表示秒
    var diff = (nowD - oldD) / 1000
    var str = ''

    if (diff < 1) {
        return '刚刚'
    } else if(diff >1 && diff<60){
        diff = parseInt(diff)
        str= '秒前'
    }else if(diff/60 >=1 && diff/60<60){
        diff = parseInt(diff / 60)
        str = '分钟前'
    }else if (diff/60/60 >= 1 && diff/60/60 < 24) {
        diff = parseInt(diff / 60/60)
        str = '小时前'
    }else if (diff/60/60/24 >= 1) {
        diff = parseInt(diff /60/60/ 24)
        str = '天前'
    }

    return diff + str
}

/**
  * 快速获取当前时间
  * @param {String} str 
  * @returns String
  */
function format(str)
{
    var d = new Date()
    str = str.replaceAll('Y', d.getFullYear())
    str = str.replaceAll('m', d.getMonth()+1)
    str = str.replaceAll('d', d.getDate())
    str = str.replaceAll('H', d.getHours())
    str = str.replaceAll('i', String(d.getMinutes()).padStart(2, 0))
    str = str.replaceAll('s', String(d.getSeconds()).padStart(2, 0))
    // var y = d.getFullYear()
    // var m = d.getMonth() + 1 // 0~11
    // var r = d.getDate() 
    // var h = d.getHours()
    // var i = String(d.getMinutes()).padStart(2, 0)
    // var s = String(d.getSeconds()).padStart(2, 0)

    // 语法：字符串.padStart(个数, 默认数据)
    // 作用：字符串不勾指定个数则填充默认数据
    return str
}

/**
 *  获取随机16进制颜色
 * @returns String
 */
function randomColor(){
    var arr = [0,1,2,3,4,5,6,7,8,9, 'A', 'B', 'C', 'D', 'E', 'F']
    var max=arr.length-1
    var min=0
    var one   = arr[Math.floor(Math.random() * (max-min+1)+min)]
    var two   = arr[Math.floor(Math.random() * (max-min+1)+min)]
    var three = arr[Math.floor(Math.random() * (max-min+1)+min)]
    var four  = arr[Math.floor(Math.random() * (max-min+1)+min)]
    var five  = arr[Math.floor(Math.random() * (max-min+1)+min)]
    var six   = arr[Math.floor(Math.random() * (max-min+1)+min)]
    return '#'+one+two+three+four+five+six
}

/**
 *  字符串补0【传递来的数字长度不足2位则在左侧补0
 * @param{String|Number} date
 * @returns 过滤后的数据
 */

function zeroize(date){
    return String(date).length === 1 ? '0'+date : date
}

/**
 *  判断现在的浏览器
 * @returns string
 */
function getBrowser(){
    if (navigator.userAgent.indexOf('Chrome') !== -1){
        return '谷歌浏览器'
    }
    else if (navigator.userAgent.indexOf('Firefox') !== -1){
        return '火狐浏览器'
    }
    else if (navigator.userAgent.indexOf('Edge') !== -1){
        return 'Edge浏览器'
    }   
}

/**
 *  判断网站在PC还是移动端打开
 * @returns string
 */
function getOS() {
    var sUserAgent = navigator.userAgent.toLowerCase()
    var bIsIpad = sUserAgent.match(/ipad/i) == "ipad"
    var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os"
    var bIsAndroid = sUserAgent.match(/android/i) == "android"
    var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce"
    var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile"

    if (bIsIpad || bIsIphoneOs || bIsAndroid || bIsCE || bIsWM) {
        return 'mobile'
    } else {return 'pc'}   
}


/**
 *  判断是否微信内置浏览器
 * @returns string
 */
function isWx() {
    var ua = window.navigator.userAgent.toLowerCase();
    
    if (ua.match(/MicroMessenger/i) == 'micromessenger') {
        return '微信内置浏览器';
    } else {
        return '非微信内置浏览器';
    }
}

/**
 *  获取标签样式
 * @param {object} tagObj 
 * @param {string} cssAttr 
 * @returns string
 */
function getStyle(tagObj,cssAttr){
    if(window.getComputedStyle){
        return getComputedStyle(tagObj)[cssAttr]
    }
    else{
        return tagObj.currentStyle[cssAttr]
    }
}

/**
 *  仿写JSON内置API的parse方法
 *  @param {String} javaData JSON数据格式
 *  @returns Object
 */
function parse(javaData)
{
    //1 去掉【" { } 空格】  
    let str = javaData.replace(/"|{|}|\s/g,'')
    let arr = str.split(',')
    //4 遍历上面：定义空对象
    let obj = {}  
    //3 遍历 
    arr.forEach(item => {
        //5 遍历里面：按照【:】分割
        let temp = item.split(':')
        //6 遍历里面：数组的第一个值就是键，数组的第二个值就是值
        //obj.键 = 值
        //obj.temp[0] = temp[1]
        obj[temp[0]] = isNaN(Number(temp[1])) ? temp[1] : Number(temp[1])
    })
    return obj
}

/**
   * 仿写JSON内置API的stringify方法
   * @param {Object} obj 标准普通对象
   * @returns String
   */
function stringify(obj)
{
    //1 定义空数组
    let arr = []
    //2 遍历对象，将【键、值】push到数组中
    for (let key in obj)
    {
        let val = obj[key]
        if (!['boolean', 'number'].includes(typeof val)) 
        {
            val = `"${obj[key]}"`
        }
        arr.push(`"${key}":${val}`)
    }
    //3 拼接成json数据格式
    return `{${arr.join(',')}}`
}

